package net.huashitong.supervision.task.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.task.dao.TimeLimitDao;
import net.huashitong.supervision.task.entity.*;
import net.huashitong.supervision.task.filter.SpecialTaskFilter;
import net.huashitong.supervision.task.service.SpecialService.TaskSpecialService;
import net.huashitong.supervision.task.util.HolidayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/9/16
 * Time: 20:40
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class TimeLimitService extends StringPKService<TimeLimit> {
    @Autowired
    private TimeLimitDao timeLimitDao;
    @Autowired
    private SysTableService tableService;
    @Lazy
    @Autowired
    private TaskMainService taskMainService;
    @Lazy
    @Autowired
    private TaskSubService taskSubService;
    @Lazy
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private SpecialTaskService specialService;

    @Override
    protected BaseDao<TimeLimit, String> getDao() {
        return timeLimitDao;
    }

    @Override
    protected void beforeSave(TimeLimit obj) {
        obj.setSysId(tableService.updateMaxSysId("t_time_limit",null));
    }
    public void save(TimeLimit obj, String userId){
        Date date = new Date();
        String date1 = HolidayUtils.sdf1.format(date);
        String time = HolidayUtils.sdf2.format(date);
        obj.setSysId(tableService.updateMaxSysId("t_time_limit",null));
        obj.setCreateUser(userId);
        obj.setUpdUser(userId);
        obj.setCreateDate(date1);
        obj.setCreateTime(time);
        obj.setUpdDate(date1);
        obj.setUpdTime(time);
        getDao().save(obj);
    }

    /**
     * @param state
     * @return
     */
    public List<TimeLimit> queryState(String state) {
        return timeLimitDao.queryState(state);
    }

    public List<TimeLimit> getBySubId(String taskSubId) {
        return timeLimitDao.getBySubId(taskSubId);
    }
    public List<TimeLimit> getBySubIdDel(String taskSubId) {
        return timeLimitDao.getBySubIdDel(taskSubId);
    }

    /**
     * 根据子任务id删除时限表
     * @param subId
     */
    public void deleteBySubId(String subId) {
        timeLimitDao.deleteBySubId(subId);
    }


    /**
     * 重大事项-终止所有任务
     * @param taskMainId
     */
    public void updateTask(String taskMainId){

        List<TaskSub> subLists = taskSubService.getByMainId(taskMainId);

        for (TaskSub subTask : subLists) {

            List<TimeLimit> timeLimits = timeLimitDao.getbyTaskId( null, subTask.getSysId(),null);
            if(ObjectUtils.isNotBlank(timeLimits))
            {
                timeLimits.forEach(timeLimit -> timeLimit.setState("0"));
            }
            this.batchUpdate(timeLimits);
        }

        SpecialTaskFilter filter = new SpecialTaskFilter();
        filter.setTaskMainId(taskMainId);
        List<SpecialTask> specialTasks = specialService.queryFilter(filter);
        for (SpecialTask item : specialTasks) {
            List<TimeLimit> timeLimits = timeLimitDao.getbyTaskId( item.getSysId(),null,null);
            if(ObjectUtils.isNotBlank(timeLimits))
            {
                timeLimits.forEach(timeLimit -> timeLimit.setState("0"));
            }
            this.batchUpdate(timeLimits);
        }

        List<TaskUnit> taskunits = taskUnitService.getByMainId(taskMainId);
        for (TaskUnit item : taskunits) {
            List<TimeLimit> timeLimits = timeLimitDao.getbyTaskId(null,null, item.getSysId());
            if(ObjectUtils.isNotBlank(timeLimits))
            {
                timeLimits.forEach(timeLimit -> timeLimit.setState("0"));
            }
            this.batchUpdate(timeLimits);
        }

    }



}
